package.path = "./plugin/TQS601D/utils.lua" .. ";..\\?.lua"
package.path = "./plugin/TQS601D/json.lua" .. ";..\\?.lua"

require "utils.lua"
require "json.lua"

rxBuf = {}
regAddr = 0x0000

function NewVariables()
    -- body
    local VariableMap = {
        Variable = {
        },
    }

    return VariableMap
end

function GenerateGetRealVariables(sAddr, step)

    -- body
    local crc
    local requestADU = {}
    local VariableMap = {
        Status = "0",
        Variable = {},
    }

    if (step == 0)
    then
        -----------温湿度----------------
        requestADU[1] = sAddr
        requestADU[2] = 0x03
        requestADU[3] = 0x00
        requestADU[4] = 0x00
        requestADU[5] = 0x00
        requestADU[6] = 0x02
        local VariableMapTemp = {
            Variable = requestADU
        }
        crc = GetCRCModbus(VariableMapTemp)
        requestADU[7] = math.modf(crc % 256)
        requestADU[8] = math.modf(crc / 256)

        regAddr = 0x0000
        VariableMap = {
            Status = "1",
            Variable = requestADU
        }
    elseif (step==1)
    then
        -----------NTCw温度----------------
        requestADU[1] = sAddr
        requestADU[2] = 0x03
        requestADU[3] = 0x00
        requestADU[4] = 0x06
        requestADU[5] = 0x00
        requestADU[6] = 0x01
        local VariableMapTemp = {
            Variable = requestADU
        }
        crc = GetCRCModbus(VariableMapTemp)
        requestADU[7] = math.modf(crc % 256)
        requestADU[8] = math.modf(crc / 256)

        regAddr = 0x0006
        VariableMap = {
            Status = "1",
            Variable = requestADU
        }
    elseif(step==2) 
    then
        -----------电能----------------
        requestADU[1] = sAddr
        requestADU[2] = 0x03
        requestADU[3] = 0x00
        requestADU[4] = 0x80
        requestADU[5] = 0x00
        requestADU[6] = 0x0E
        local VariableMapTemp = {
            Variable = requestADU
        }
        crc = GetCRCModbus(VariableMapTemp)
        requestADU[7] = math.modf(crc % 256)
        requestADU[8] = math.modf(crc / 256)

        regAddr = 0x00
        VariableMap = {
            Status = "0",
            Variable = requestADU
        }
    end
    return VariableMap
end

function DeviceCustomCmd(sAddr, cmdName, cmdParam, step)

    -- body
    local crc
    local requestADU = {}
    local VariableMap = {
        Status = "0",
        Variable = {},
    }
    local temp = 0
    if (cmdName == "GetRealVariables")
    then
        if (step == 0)
        then
            -----------温湿度----------------
            requestADU[1] = sAddr
            requestADU[2] = 0x03
            requestADU[3] = 0x00
            requestADU[4] = 0x00
            requestADU[5] = 0x00
            requestADU[6] = 0x02
            local VariableMapTemp = {
                Variable = requestADU
            }
            crc = GetCRCModbus(VariableMapTemp)
            requestADU[7] = math.modf(crc % 256)
            requestADU[8] = math.modf(crc / 256)
    
            regAddr = 0x0000
            VariableMap = {
                Status = "1",
                Variable = requestADU
            }
        elseif (step==1)
        then
            -----------NTCw温度----------------
            requestADU[1] = sAddr
            requestADU[2] = 0x03
            requestADU[3] = 0x00
            requestADU[4] = 0x06
            requestADU[5] = 0x00
            requestADU[6] = 0x01
            local VariableMapTemp = {
                Variable = requestADU
            }
            crc = GetCRCModbus(VariableMapTemp)
            requestADU[7] = math.modf(crc % 256)
            requestADU[8] = math.modf(crc / 256)
    
            regAddr = 0x0006
            VariableMap = {
                Status = "1",
                Variable = requestADU
            }
        elseif(step==2) 
        then
            -----------电能----------------
            requestADU[1] = sAddr
            requestADU[2] = 0x03
            requestADU[3] = 0x00
            requestADU[4] = 0x80
            requestADU[5] = 0x00
            requestADU[6] = 0x0E
            local VariableMapTemp = {
                Variable = requestADU
            }
            crc = GetCRCModbus(VariableMapTemp)
            requestADU[7] = math.modf(crc % 256)
            requestADU[8] = math.modf(crc / 256)
    
            regAddr = 0x00
            VariableMap = {
                Status = "0",
                Variable = requestADU
            }
        end
    elseif (cmdName == "SetOpen")
    then
        -----------合闸----------------
        requestADU[1] = sAddr
        requestADU[2] = 0x06
        requestADU[3] = 0x00
        requestADU[4] = 0x00
        requestADU[5] = 0x00
        requestADU[6] = 0x01
        local VariableMapTemp = {
            Variable = requestADU
        }
        crc = GetCRCModbus(VariableMapTemp)
        requestADU[7] = math.modf(crc % 256)
        requestADU[8] = math.modf(crc / 256)

        regAddr = 0x0000
        VariableMap = {
            Status = "0",
            Variable = requestADU
        }
    elseif (cmdName == "SetClose")
    then
        -----------分闸----------------
        requestADU[1] = sAddr
        requestADU[2] = 0x06
        requestADU[3] = 0x00
        requestADU[4] = 0x00
        requestADU[5] = 0x00
        requestADU[6] = 0x00
        local VariableMapTemp = {
            Variable = requestADU
        }
        crc = GetCRCModbus(VariableMapTemp)
        requestADU[7] = math.modf(crc % 256)
        requestADU[8] = math.modf(crc / 256)

        regAddr = 0x0000
        VariableMap = {
            Status = "0",
            Variable = requestADU
        }
    end

    return VariableMap
end

function AnalysisRx(sAddr, rxBufCnt)

    local index = 1
    local mAddr = 0
    local mFunCode = 0
    local mRegByte = 0
    local crcH, crcL, crc, crcTemp

    -- body
    local VariableMap = {
        Status = "1",
        Variable = {},
    }

    --modbus03 06 最短为8个字节
    if (rxBufCnt < 8)
    then
        rxBuf = {}
        return VariableMap
    end


    for index = 1, rxBufCnt, 1 do
        if (index <= rxBufCnt)
        then
            mAddr    = rxBuf[index]
            mFunCode = rxBuf[index + 1]
            if (mFunCode == 0x03)
            then
                mRegByte = rxBuf[index + 2]
                if (mRegByte + 5 > rxBufCnt)
                then
                    rxBuf = {}
                    return VariableMap
                end
                --取出数据域数据
                local pdu = {}
                for i = index, index + 2 + mRegByte, 1 do
                    if rxBuf[i] ~= nil then
                        table.insert(pdu, rxBuf[i])
                    end
                end
                local VariableMapCRC = {
                    Variable = pdu
                }
                crc = GetCRCModbus(VariableMapCRC)
                crcTemp = rxBuf[index + 3 + mRegByte + 1] * 256 + rxBuf[index + 3 + mRegByte]
                print(crc)
                if (crc == crcTemp)
                then
                    ---------温湿度------------
                    local Variable = {}
                    local VariableIndex = 1
                    if (regAddr == 0x0000)
                    then
                        -----------------------------温度--------------------------------
                        local tempValue = rxBuf[index + 3] * 256 + rxBuf[index + 4]
                        Variable[VariableIndex] = utils.AppendVariable(VariableIndex, "Humi",tempValue,tempValue)
                        VariableIndex = VariableIndex + 1
                        -----------------------------湿度--------------------------------
                        local HumiValue = rxBuf[index + 5] * 256 + rxBuf[index + 6]
                        Variable[VariableIndex] = utils.AppendVariable(VariableIndex, "Temp",HumiValue,HumiValue)
                        VariableIndex = VariableIndex + 1

                        VariableMap = {
                            Status = "0", --0 解析成功，1解析失败
                            Variable = Variable
                        } 
                    elseif (regAddr==0x0006)
                    then
                        -----------------------------NTC温度--------------------------------
                        local thermalValue = rxBuf[index + 3] * 256 + rxBuf[index + 4]
                        Variable[VariableIndex] = utils.AppendVariable(VariableIndex, "Thermal",thermalValue,thermalValue)
                        VariableIndex = VariableIndex + 1
                        VariableMap = {
                            Status = "0", --0 解析成功，1解析失败
                            Variable = Variable
                        } 
                    elseif (regAddr==0x0080)
                    then
                        -----------------------------A相电压--------------------------------
                        local AVoltage = rxBuf[index + 3] * 256 + rxBuf[index + 4]
                        Variable[VariableIndex] = utils.AppendVariable(VariableIndex, "AVoltage",AVoltage,AVoltage)
                        VariableIndex = VariableIndex + 1
                        -----------------------------B相电压--------------------------------
                        local BVoltage = rxBuf[index + 5] * 256 + rxBuf[index + 6]
                        Variable[VariableIndex] = utils.AppendVariable(VariableIndex, "BVoltage",BVoltage,HumiBVoltageValue)
                        VariableIndex = VariableIndex + 1
                        -----------------------------C相电压--------------------------------
                        local CVoltage = rxBuf[index + 7] * 256 + rxBuf[index + 8]
                        Variable[VariableIndex] = utils.AppendVariable(VariableIndex, "CVoltage",CVoltage,CVoltage)
                        VariableIndex = VariableIndex + 1
                        -----------------------------A相电流--------------------------------
                        local Acurrent = rxBuf[index + 9] * 256 + rxBuf[index + 10]
                        Variable[VariableIndex] = utils.AppendVariable(VariableIndex, "Acurrent",Acurrent,string.format("%2.1f", Acurrent / 10.0))
                        VariableIndex = VariableIndex + 1
                        -----------------------------B相电流--------------------------------
                        local Bcurrent = rxBuf[index + 11] * 256 + rxBuf[index + 12]
                        Variable[VariableIndex] = utils.AppendVariable(VariableIndex, "Bcurrent",Bcurrent,string.format("%2.1f", Bcurrent / 10.0))
                        VariableIndex = VariableIndex + 1
                        -----------------------------C相电流--------------------------------
                        local Ccurrent = rxBuf[index + 13] * 256 + rxBuf[index + 14]
                        Variable[VariableIndex] = utils.AppendVariable(VariableIndex, "Ccurrent",Ccurrent,string.format("%2.1f", Ccurrent / 10.0))
                        VariableIndex = VariableIndex + 1
                        -----------------------------A相电能--------------------------------
                        local AElec = rxBuf[index+ 15]*16777216+rxBuf[index+16]*65536+rxBuf[index+17]*256+rxBuf[index+18]
                        Variable[VariableIndex] = utils.AppendVariable(VariableIndex, "AElec",AElec,string.format("%2.1f", AElec / 10.0))
                        VariableIndex = VariableIndex + 1
                        -----------------------------B相电能--------------------------------
                        local BElec =  rxBuf[index+ 19]*16777216+rxBuf[index+20]*65536+rxBuf[index+21]*256+rxBuf[index+22]
                        Variable[VariableIndex] = utils.AppendVariable(VariableIndex, "BElec",BElec,string.format("%2.1f", BElec / 10.0))
                        VariableIndex = VariableIndex + 1
                        -----------------------------C相电能--------------------------------
                        local CElec = rxBuf[index+ 23]*16777216+rxBuf[index+24]*65536+rxBuf[index+25]*256+rxBuf[index+26]
                        Variable[VariableIndex] = utils.AppendVariable(VariableIndex, "CElec",CElec,string.format("%2.1f", CElec / 10.0))
                        VariableIndex = VariableIndex + 1
                        -----------------------------总电能--------------------------------
                        local AllElec = rxBuf[index+ 27]*16777216+rxBuf[index+28]*65536+rxBuf[index+29]*256+rxBuf[index+30]
                        Variable[VariableIndex] = utils.AppendVariable(VariableIndex, "AllElec",AllElec,string.format("%2.1f", AllElec / 10.0))
                        VariableIndex = VariableIndex + 1

                        VariableMap = {
                            Status = "0", --0 解析成功，1解析失败
                            Variable = Variable
                        } 
                    end
                    rxBuf = {}
                    return VariableMap
                end
            else
                index = index + 1
            end
        else
            rxBuf = {}
            return VariableMap
        end
    end

    rxBuf = {}
    return VariableMap
end
